package com.sn.system.file;

import java.io.*;

public class FileUtil {
	// accessMode
	public static int F_READ = 1;
	public static int F_WRITE = 2;

	// createFlag:
	// Creates a new file. The function fails if the specified file already exists.
	public static int CREATE_NEW = 1;
	// Creates a new file. If the file exists, the function overwrites the file and clears the existing attributes.
	public static int CREATE_ALWAYS = 2;
	// Opens the file. The function fails if the file does not exist.
	public static int OPEN_EXISTING = 3;
	// Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
	public static int OPEN_ALWAYS = 4;
	// Opens the file. Once opened, the file is truncated so that its size is zero bytes. The function fails if the file does not exist.
	public static int TRUNCATE_EXISTING = 5;

	public void createFile(String fileName, int accessMode, int createFlag) {
		this.fileName = fileName;
		this.accessMode = accessMode;
		this.createFlag = createFlag;
	}

	public void close() {

	}

	public String readLine() {
		return null;
	}

	public void write(String text) {

	}

	public void writeLine(String lineText) {

	}


	public static long getFileSize(String fileName) {
		File f = new File(fileName);
		return f.length();
	}

	public static boolean rename(String srcFileName, String destFileName) {
		File src = new File(srcFileName);
		File dest = new File(destFileName);
		return src.renameTo(dest);
	}

	public static boolean delete(String fileName) {
		File f = new File(fileName);
		return f.delete();
	}

	private String fileName;
	private int accessMode;
	private int createFlag;
	
	
	public static void makePath(String path){
		try {
			File pfile=new File(path);
			if(!pfile.exists()){
				boolean ok=pfile.mkdirs();
				//System.out.println(ok) ;
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static void delFile(File file) {
		if (!file.exists()) {                      
		System.out.println("file not exist ");
		return;
		}
		boolean rs = file.delete();              
		if (rs)
		System.out.println("delete temp file success");
		else
		System.out.println("delete temp file fail");
		}
	
	public static InputStream getDownloadFile(String realPath, byte[] fileContents) throws Exception {  
	      
	    InputStream stream = null;  
	      
	    if (realPath == null && fileContents == null) {  
	        throw new Exception("not found stream Contents");  
	    }  	  
	    if (realPath != null && !"".equals(realPath)) {  
	          
	        try {  
	            File readFile = new File(realPath);  
	            FileInputStream fis = new FileInputStream(readFile);  
	              
	            byte[] buffer = new byte[(int)readFile.length()];  
	            fis.read(buffer);  
	              
	            fis.close();  
	              
	            stream = new ByteArrayInputStream(buffer);  
	        } catch (FileNotFoundException e) {  
	            throw e;  
	        }  
	    } else {  
	        stream = new ByteArrayInputStream(fileContents);  
	    }  
	      
	    return stream;  
	}  
	
}
